/**
 * @file eqEvent事件查询
 */
// import dynamicMenu from '@/components/Menu/menu.vue';
import myTree from "@/components/tree";
import timeSelect from "@/components/app-time-select/index.vue";

export default {
    components: { myTree, timeSelect },
    data() {
        return {
            tableHeight: 0,
            tableHeightApp: 300,
            status: this.$t("eqEvent.tips[4]"),
            light: {},
            searchTime: [],
            eventType: "systemQuery",
            eventTypeList: [
                {
                    type: "E",
                    name: "eqEvent.typeJs[0]"
                },
                {
                    type: "C",
                    name: "eqEvent.typeJs[1]"
                },
                {
                    type: "X",
                    name: "eqEvent.typeJs[2]"
                },
                {
                    type: "S",
                    name: "eqEvent.typeJs[3]"
                },
                {
                    type: "systemQuery",
                    name: "eqEvent.typeJs[4]"
                }
            ],
            tbLoad: false,
            eqData: [],
            nodeLoad: false,
            beginTime: this.$moment()
                .startOf("month")
                .format("YYYY-MM-DD HH:mm:ss"),
            endTime: this.$moment()
                .endOf("month")
                .format("YYYY-MM-DD HH:mm:ss"),
            scout: "",
            eqSum: 0,
            defaultProps: {
                children: "children",
                label: "title"
            },
            recordPage: {
                pageSize: 10,
                pageNo: 1,
                total: 1
            },
            eqPage: {
                pageNo: 1,
                pageSize: 25,
                total: 1
            },
            tableData: [],
            allStatus: {},
            eqChoice: [],
            isScout: false,
            equipList: [],
            searchArray: [],
            eventName: "",
            pickerOptions: {
                shortcuts: [
                    {
                        text: this.$t("eqEvent.tips[1]"),
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                start.getTime() - 3600 * 1000 * 24 * 7
                            );
                            picker.$emit("pick", [start, end]);
                        }
                    },
                    {
                        text: this.$t("eqEvent.tips[2]"),
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                start.getTime() - 3600 * 1000 * 24 * 30
                            );
                            picker.$emit("pick", [start, end]);
                        }
                    },
                    {
                        text: this.$t("eqEvent.tips[3]"),
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                start.getTime() - 3600 * 1000 * 24 * 90
                            );
                            picker.$emit("pick", [start, end]);
                        }
                    }
                ]
            },

            // app
            drawerAppHeader: false,
            mainAppShow: false,
            appStartTimeValue: "",
            appEndTimeValue: "",
            showTip: false,
            equipArr: [],
            getDataUrl: "evtList",
            dataList: [], // 查询结果列表
            showDataDialog: false,
            searBtnLoading: false,
            hasData: false,
            showDataDetail: false,
            dataItem: {},
            showBottom: true
        };
    },
    mounted() {
        this.beginTime = this.myUtils.getCurrentDate(1, "-") + " 00:00:00";
        this.endTime = this.myUtils.getCurrentDate(1, "-") + " 23:59:59";
        this.searchTime.push(this.myUtils.getCurrentDate(1, "-") + " 00:00:00");
        this.searchTime.push(this.myUtils.getCurrentDate(1, "-") + " 23:59:59");

        this.appStartTimeValue =
            this.myUtils.getCurrentDate(1, "-") + " 00:00:00";
        this.appEndTimeValue =
            this.myUtils.getCurrentDate(1, "-") + " 23:59:59";
        this.getEqList(false);
    },
    methods: {
        // app
        getBack() {
            this.mainAppShow = false;
        },
        showDetail(item) {
            this.showDataDetail = true;
            this.dataItem = item;
        },
        closeDetail() {
            this.showDataDetail = false;
        },
        onQuery() {
            console.log(this.$refs.myTree.getCheckedNodes());
            this.getEquipChoice(this.$refs.myTree.getCheckedNodes());
            if (this.eqChoice.length == 0 && this.eventType != "systemQuery") {
                this.showTip = true;
            } else {
                this.showTip = false;
            }
            this.dataList = [];
            this.drawerAppHeader = true;
        },
        getEquipChoice(list) {
            this.eqChoice = [];
            for (const item of list) {
                if (!item.isGroup) {
                    this.eqChoice.push(item.key);
                }
            }
        },

        closeDrawerAppHeader() {
            this.drawerAppHeader = false;
            this.showTip = false;
        },
        closeDrawer() {},
        appStartTime() {
            this.searchTime[0] = this.appStartTimeValue;
            this.beginTime = this.appStartTimeValue;
        },
        appEndTime() {
            this.endTime = this.appEndTimeValue;
            this.searchTime[1] = this.appEndTimeValue;
        },

        // 选择事件类型
        changeTypeList(val) {
            if (this.eqChoice.length == 0 && this.eventType != "systemQuery") {
                this.showTip = true;
                return;
            }
            this.showTip = false;

            this.eventType = val;
            this.recordPage.pageNo = 1;
            this.dataList = [];
            this.hasData = false;
            this.dataItem = {};
        },

        // 选择时间跨度
        checkTime(searchTime) {
            if (searchTime === null) {
                this.beginTime = "";
                this.endTime = "";
            } else if (searchTime.length === 2) {
                this.beginTime = this.$moment(searchTime[0]).format(
                    "YYYY-MM-DD HH:mm:ss"
                );
                this.endTime = this.$moment(searchTime[1]).format(
                    "YYYY-MM-DD HH:mm:ss"
                );
                this.appStartTimeValue = this.$moment(searchTime[0]).format(
                    "YYYY-MM-DD HH:mm:ss"
                );
                this.appEndTimeValue = this.$moment(searchTime[1]).format(
                    "YYYY-MM-DD HH:mm:ss"
                );
            }
        },

        inputChange() {
            this.scout = this.scout.trim();
            if (!this.scout) {
                this.getEqList(true);
            }

            // if (this.scout === '') {
            //     this.isScout = false;
            //     this.status = this.$t('eqEvent.tips[4]');
            //     this.light = {};
            //     this.eqChoice = [];
            //     this.nodeLoad = false;

            // } else {
            //     this.isScout = true;
            //     this.nodeLoad = true;
            //     this.eqPage.pageNo = 1;
            //     this.eqPage.pageSize = 20;
            // }
        },

        // 搜索设备
        scoutEq() {
            this.getEqList(true);
        },

        // 获取设备列表及设备状态
        getEqList(reflesh) {
            /**
             *  缓存
             */
            let that = this;

            // if (that.globalVariable.initsEquipAList && that.globalVariable.initsEquipAList.groups && !reflesh) {
            //     that.nodeLoad = true;
            //     setTimeout(function () {
            //         that.nodeLoad = false;
            //         that.equipList = that.globalVariable.initsEquipAList.groups;
            //     }, 500);
            //     return;
            // }

            this.nodeLoad = true;
            let data = {
                pageNo: this.eqPage.pageNo,
                pageSize: 25,
                SearchName: this.scout
            };
            let equipList = [];

            this.$api
                .getEquipLists(data)
                .then(rt => {
                    let result = rt.data;
                    that.nodeLoad = false;
                    that.equipList = [];

                    // 搜索请求结果
                    if (result.code === 200) {
                        that.totalLeft = result.data.count; // 左侧总页码
                        equipList = result.data.groups;
                        that.equipList = equipList;
                        this.equipList = this.formateList(equipList);
                        console.log(this.equipList);
                    } else {
                        this.$message({
                            message: this.result.message,
                            type: "warning"
                        });
                    }
                })
                .catch(() => {
                    that.nodeLoad = false;

                    // this.$message.error('服务错误!');
                });
        },
        formateList(list) {
            let arr = [];
            for (const item of list) {
                let dataItem = {};
                dataItem.count = item.count;
                dataItem.title = item.groupName;
                dataItem.key = item.groupId;
                dataItem.isGroup = true;
                dataItem.children = [];
                dataItem.status = 1;
                if (item.equipLists.length > 0) {
                    dataItem.children = [
                        ...dataItem.children,
                        ...item.equipLists.map(equip => {
                            return {
                                count: 0,
                                title: equip.equipName,
                                equipState: equip.equipState,
                                key: equip.equipNo,
                                staNo: equip.staNo,
                                status: equip.equipState
                            };
                        })
                    ];
                }
                if (item.children.length > 0) {
                    dataItem.children = [
                        ...dataItem.children,
                        ...this.formateList(item.children)
                    ];
                }

                arr.push(dataItem);
            }
            return arr;
        },

        radioCheck(arr) {
            this.equipArr = arr;
        },

        closeDataDialog() {
            this.showDataDialog = false;
        },

        // 获取设备事件列表
        getList() {
            if (!this.endTime || !this.beginTime) {
                this.$message({
                    message: this.$t("eqEvent.tips[5]"),
                    type: "warning"
                });
                return;
            }
            if (this.eventType == "systemQuery") {
                this.getDataUrl = "evtSysList";
            } else {
                this.getDataUrl = "evtList";
            }

            this.searBtnLoading = true;
            let data;

            if (this.eventType == "systemQuery") {
                data = {
                    pageNo: this.recordPage.pageNo,
                    pageSize: this.recordPage.pageSize,
                    beginTime: this.appStartTimeValue,
                    endTime: this.appEndTimeValue,
                    sort: "desc",
                    eventName: ""
                };
            } else {
                data = {
                    pageNo: this.recordPage.pageNo,
                    pageSize: this.recordPage.pageSize,
                    beginTime: this.appStartTimeValue,
                    endTime: this.appEndTimeValue,
                    equipNos: this.eqChoice.join(","),
                    eventType: this.eventType,
                    sort: "desc",
                    eventName: this.eventName
                };
            }

            this.$api[this.getDataUrl](data)
                .then(res => {
                    if (res.data.code == 200) {
                        let arr = JSON.parse(res.data.data.list);
                        if (arr.length > 0) {
                            // this.drawerAppHeader = false;
                            this.showDataDialog = true;
                            this.dataList = [...this.dataList, ...arr];
                            this.recordPage.pageNo = this.recordPage.pageNo + 1;
                            this.hasData = true;
                        } else {
                            if (this.hasData) {
                                this.$message.warning("无最新数据");
                            } else {
                                this.$message.warning("暂无数据");
                            }
                        }
                    }
                    this.searBtnLoading = false;
                })
                .catch(err => {
                    console.log(err);
                    this.searBtnLoading = false;
                });
        },

        // 根据日期查找设备事件
        searchEvt(type) {
            if (this.eqChoice.length == 0 && this.eventType != "systemQuery") {
                return;
            }
            if (this.searchTime === "") {
                this.$message({
                    message: this.$t("eqEvent.tips[6]"),
                    type: "warning"
                });
                return;
            }

            // this.drawerAppHeader = false;
            this.recordPage.pageNo = 1;
            if (type) {
                this.mainAppShow = false;
            } else {
                this.mainAppShow = true;
            }
            this.getList();
        }
    }
};
